﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Serialization;
using Kotonoha.Objects.Model.Event;
using Kotonoha.Client.Synch;
using Container=Kotonoha.Objects.Model.Container;

namespace Kotonoha.Client.UI {
    public partial class MainForm : Form {
        private bool containerChanged;
        public const string DbPath = "./data/db.xml";

        public MainForm() {
            InitializeComponent();
            SynchronizationManager.SynchStart += MainForm_SynchStart;
            SynchronizationManager.SynchEnd += MainForm_SynchEnd;
        }

        private void MainForm_SynchEnd() {
            if (statusLabel.InvokeRequired) {
                statusLabel.Invoke(new Action(OnEnd));
            } else {
                OnEnd();
            }
        }

        private void OnEnd() {
            statusLabel.Text = "Synchronization completed";
            startBtn.Enabled = true;
            readContainer();
        }

        private void readContainer() {
            XmlSerializer ser = new XmlSerializer(typeof(Container));
            FileStream read = File.OpenRead(DbPath);
            Container = (Container) ser.Deserialize(read);
            Container.buildWordMap();
            EventList list = new EventList("./data/log");
            foreach (Event e in list.load()) {
                e.apply(Container);
            }
            read.Close();
        }

        private void MainForm_SynchStart() {
            if (statusLabel.InvokeRequired) {
                statusLabel.Invoke(new Action(OnStart));
            } else {
                OnStart();
            }
        }

        private void OnStart() {
            Container = null;
            statusLabel.Text = "Please wait, synchronizing...";
            startBtn.Enabled = false;
        }

        public Container Container { get; set; }

        private void startBtn_Click(object sender, EventArgs e) {
            if (Container == null) {
                return;
            }
            WordForm form = new WordForm();
            form.ContainerChanged += WordForm_ContainerChanged;
            form.Container = Container;
            form.Show();
        }

        private void WordForm_ContainerChanged() {
            containerChanged = true;
        }

        private void MainForm_Load(object sender, EventArgs e) {
            if (File.Exists(DbPath)) {
                readContainer();
                statusLabel.Text = "Database Loaded";
            } else {
                statusLabel.Text = "No db found, please synch";
                startBtn.Enabled = false;
            }
            saveTimer.Enabled = true;
        }

        private void saveTimer_Tick(object sender, EventArgs e) {
            /*if (Container != null && containerChanged) {
                FileStream str = File.OpenWrite(DbPath);
                XmlSerializer ser = new XmlSerializer(typeof(Container));
                ser.Serialize(str, Container);
                str.Close();
                containerChanged = false;
            }*/
        }

        private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
            saveTimer_Tick(sender, e);
        }

        private void exitBtn_Click(object sender, EventArgs e) {
            Close();
            Application.Exit();
        }

        private void MainForm_Closed(object sender, EventArgs e) {
            SynchronizationManager.SynchStart -= MainForm_SynchStart;
            SynchronizationManager.SynchEnd -= MainForm_SynchEnd;
        }
    }
}